package com.ecar.common.util;

import sun.awt.SunHints;

import java.awt.*;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.IOException;

/**
 * Created by admin on 2016/12/13.
 */
public class ImageProcesser {

	public final static String FONT_DEFAULT = "sans-serif";
	private final static Color BACK_COLOR = new Color(72, 137, 219);
	private final static Color FONT_COLOR = Color.WHITE;

	private BufferedImage image;
	private int width;
	private int height;
	private int type;

	public ImageProcesser(BufferedImage image) {
		this.image = image;
		this.width = image.getWidth();
		this.height = image.getHeight();
		this.type = image.getType();
	}

	public BufferedImage textLogo(String text, int width, int height) {
		return textLogo(text, width, height, BACK_COLOR, FONT_COLOR, FONT_DEFAULT);
	}

	public BufferedImage textLogo(String text, int width, int height, String fontFamily) {
		return textLogo(text, width, height, BACK_COLOR, FONT_COLOR, fontFamily);
	}

	public BufferedImage textLogo(String text, int width, int height, Color backColor, Color fontColor) {
		return textLogo(text, width, height, backColor, fontColor, FONT_DEFAULT);
	}

	public BufferedImage textLogo(String text, int width, int height, Color backColor, Color fontColor, String fontFamily) {
		BufferedImage logo = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
		Graphics2D graphics = logo.createGraphics();
		graphics.setColor(backColor);
		graphics.fillRect(0, 0, width, height);
		graphics.setColor(fontColor);
		graphics.setRenderingHint(SunHints.KEY_TEXT_ANTIALIASING, SunHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);

		drawText(graphics, text, width, height, fontFamily);
		logo.flush();
		return logo;
	}

	private void drawText(Graphics2D graphics, String text, int width, int height, String fontFamily) {
		int len = text.length();
		int x = 0;
		int y = 0;
		int fontSize = (height > width ? width : height) / 2;
		switch (len) {
			case 0:
				return;
			case 1:
				fontSize = fontSize * 2;
				Font font = LoadFont.loadFont(fontSize);
				graphics.setFont(font);
				graphics.setColor(FONT_COLOR);
				graphics.drawString(text, 0, height - 10);
				break;
			case 2:
				fontSize = fontSize + 5;
				y = fontSize + 12;
				graphics.setFont(LoadFont.loadFont(fontSize));
				graphics.setColor(FONT_COLOR);
				graphics.drawString(text.substring(0, 1), x, y);
				graphics.drawString(text.substring(1, 2), x + fontSize - 8, y);
				break;
			case 3:
				y = fontSize - 2;
				graphics.setFont(LoadFont.loadFont(fontSize));
				graphics.setColor(FONT_COLOR);
				graphics.drawString(text.substring(0, 2), x, y);
				graphics.drawString(text.substring(2, len), x + fontSize / 2, y * 2);
				break;
			case 4:
				y = fontSize - 2;
				graphics.setFont(LoadFont.loadFont(fontSize));
				graphics.drawString(text.substring(0, 2), x, y);
				graphics.drawString(text.substring(2, len), x, y * 2);
				break;
			case 5:
				fontSize = fontSize - 10;
				graphics.setFont(LoadFont.loadFont(fontSize));
				for (int i = 0; i < 3; i++) {
					String str = text.substring(i, i + 1);
					if (i % 3 == 0) {
						x = 0;
						y += fontSize + 5;
					} else {
						x += fontSize - 5;
					}
					graphics.drawString(str, x, y);
				}
				y += fontSize + 5;
				graphics.drawString(text.substring(3, len), fontSize / 2 - 5, y);
				break;
			case 6:
				fontSize = fontSize - 10;
				graphics.setFont(LoadFont.loadFont(fontSize));
				for (int i = 0; i < len; i++) {
					if (i % 3 == 0) {
						x = 0;
						y += fontSize + 5;
					} else {
						x += fontSize - 5;
					}
					graphics.drawString(text.substring(i, i + 1), x, y);
				}
				break;
			default:
				fontSize = fontSize - 10;
				graphics.setFont(LoadFont.loadFont(fontSize));
				for (int i = 0; i < len; i++) {
					if (i % 3 == 0) {
						x = 0;
						y += fontSize + 5;
					} else {
						x += fontSize - 5;
					}
					if (i >= 5) {
						graphics.drawString("...", x, y);
						break;
					}
					graphics.drawString(text.substring(i, i + 1), x, y);
				}
				break;
		}
		graphics.dispose();
	}

	public ImageProcesser drawCenterText(String centredText) throws IOException {
		Graphics2D graphics = image.createGraphics();

		BufferedImage logo = textLogo(centredText, width / 10, height / 10);
		//开始绘制图片
		graphics.drawImage(logo, width / 5 * 2, height / 5 * 2, width / 5, height / 5, null);//绘制

		//指定弧度的圆角矩形
		BasicStroke stroke = new BasicStroke(10, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
		graphics.setStroke(stroke);// 设置笔画对象
		RoundRectangle2D.Float round =
			new RoundRectangle2D.Float(width / 5 * 2, height / 5 * 2, width / 5 + 5, height / 5 + 5, 20, 20);
		graphics.setColor(Color.WHITE);
		graphics.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_NORMALIZE);
		graphics.draw(round);// 绘制圆弧矩形

		//设置logo 有一道灰色边框
//		BasicStroke stroke2 = new BasicStroke(1, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
//		graphics.setStroke(stroke2);// 设置笔画对象
//		RoundRectangle2D.Float round2 =
//			new RoundRectangle2D.Float(matrixWidth / 5 * 2 + 2, matrixHeigh / 5 * 2 + 2,
//									   matrixWidth / 5 - 4, matrixHeigh / 5 - 4, 20, 20);
//		graphics.setColor(new Color(128,128,128));
//		graphics.draw(round2);// 绘制圆弧矩形
		graphics.dispose();
		image.flush();
		return this;
	}

	public ImageProcesser zoom(int scaleWidth, int scaleHeight) {
		Image originImage = image.getScaledInstance(scaleWidth, scaleHeight, Image.SCALE_DEFAULT);
		BufferedImage scaled = new BufferedImage(scaleWidth, scaleHeight, image.getType());
		Graphics graphics = scaled.getGraphics();
		graphics.drawImage(originImage, 0, 0, null);
		graphics.dispose();
		scaled.flush();
		image = scaled;
		return this;
	}

	public BufferedImage done() {
		return this.image;
	}
}
